package org.serverct.finalfight.game.hadesgame.events;

import org.bukkit.Material;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.serverct.finalfight.game.hadesgame.EventType;
import org.serverct.finalfight.internal.game.basic.BasicGame;
import org.serverct.finalfight.internal.game.hadesgame.ContinuousEvent;

public class SmoothSurface extends ContinuousEvent implements Listener {

    public SmoothSurface(BasicGame game, EventType type) {
        super(game, type);
    }

    @EventHandler
    public void onUse(PlayerInteractEvent event) {
        if (!event.hasItem()) {
            return;
        }
        Player player = event.getPlayer();
        if (!this.game.players.containsKey(player.getUniqueId())) {
            return;
        }
        if (!this.game.isAlive(player)) {
            return;
        }
        PlayerInventory inv = player.getInventory();
        ItemStack item = new ItemStack(Material.AIR);
        if (event.getHand() == EquipmentSlot.HAND) {
            item = inv.getItemInMainHand();
            inv.setItemInMainHand(null);
        }
        if (event.getHand() == EquipmentSlot.OFF_HAND) {
            item = inv.getItemInOffHand();
            inv.setItemInOffHand(null);
        }
        Item drop = player.getWorld().dropItemNaturally(player.getLocation(), item);
        drop.setPickupDelay(60);
    }

    @EventHandler
    public void onPlace(PlayerInteractEvent event) {
        Player player = event.getPlayer();
        if (!this.game.players.containsKey(player.getUniqueId())) {
            return;
        }
        if (!this.game.isAlive(player)) {
            return;
        }
        event.setCancelled(true);
    }
}
